package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 创建通知请求DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "创建通知请求参数")
public class CreateNoticeRequest {

    @NotBlank(message = "通知标题不能为空")
    @Size(min = 1, max = 200, message = "通知标题长度在1-200字符之间")
    @Schema(description = "通知标题", example = "关于春季学期开学的通知", requiredMode = Schema.RequiredMode.REQUIRED)
    private String title;

    @NotNull(message = "通知类型ID不能为空")
    @Positive(message = "通知类型ID必须是正整数")
    @Schema(description = "通知类型ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long typeId;

    @Size(max = 500, message = "通知描述长度不能超过500字符")
    @Schema(description = "通知简要描述", example = "新学期开学安排和注意事项")
    private String description;

    @NotBlank(message = "通知内容不能为空")
    @Size(max = 10000, message = "通知内容长度不能超过10000字符")
    @Schema(description = "通知详细内容", example = "<p>新学期将于3月1日正式开学...</p>")
    private String content;

    @Positive(message = "关联课程ID必须是正整数")
    @Schema(description = "关联的课程ID", example = "5")
    private Long scheduleId;

    @NotNull(message = "接收人信息不能为空")
    @Schema(description = "通知接收人配置", requiredMode = Schema.RequiredMode.REQUIRED)
    private NoticeRecipients recipients;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "通知接收人配置")
    public static class NoticeRecipients {

        @Schema(description = "班级ID列表", example = "[1, 2, 3]")
        private List<Long> classes;

        @Schema(description = "用户ID列表", example = "[101, 102, 103]")
        private List<Long> users;

        @Schema(description = "角色列表", example = "[\"student\", \"teacher\"]")
        private List<String> roles;
    }

    /**
     * 验证接收人配置是否有效
     */
    public boolean hasValidRecipients() {
        return recipients != null && (
                (recipients.classes != null && !recipients.classes.isEmpty()) ||
                        (recipients.users != null && !recipients.users.isEmpty()) ||
                        (recipients.roles != null && !recipients.roles.isEmpty())
        );
    }

    /**
     * 获取接收人总数估算
     */
    @Schema(description = "预计接收人数", accessMode = Schema.AccessMode.READ_ONLY)
    public int getEstimatedRecipientCount() {
        int count = 0;
        if (recipients != null) {
            if (recipients.classes != null) count += recipients.classes.size();
            if (recipients.users != null) count += recipients.users.size();
            if (recipients.roles != null) count += recipients.roles.size();
        }
        return count;
    }

    /**
     * 验证角色是否有效
     */
    public boolean hasValidRoles() {
        if (recipients == null || recipients.roles == null) {
            return true;
        }
        List<String> validRoles = List.of("student", "teacher", "admin", "parent");
        return recipients.roles.stream().allMatch(validRoles::contains);
    }
}
